<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : SmsService.php
 * Created on : 2020-10-23 17:44:18
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\service;

/**
 * 短信服务
 * Class SmsService
 * Tiger <1192851302@qq.com> 2020-10-23 17:44:18
 */
class SmsService {

    // 短信请求地址
    private static $SMSUrl = 'http://sms.qunai.net';

    //验证码
    const VERIFICATION_CODE = '您的验证码为{code},在{time}秒内有效.';
    //支付成功
    const PAY_SUCCESS_CODE = '订单支付成功,单号{order_id}';
    //发货提醒
    const DELIVER_GOODS_CODE_EXPRESS = '您的订单已发货,订单号{order_id},物流{delivery_name},单号{delivery_id}';
    //送货提醒
    const DELIVER_GOODS_CODE_SEND = '您的订单已发货,订单号{order_id},送货人{delivery_name},电话{delivery_id}';
    //确认收货提醒
    const TAKE_DELIVERY_CODE_EXPRESS = '您的订单已确认收货,订单号{order_id},物流{delivery_name},单号{delivery_id}';
    //确认收货提醒
    const TAKE_DELIVERY_CODE_SEND = '您的订单已确认收货,订单号{order_id},送货人{delivery_name},电话{delivery_id}';
    //管理员下单提醒
    const ADMIN_PLACE_ORDER_CODE = '管理员{admin_name},您有新的订单,已下单,单号{order_id}';
    //管理员退货提醒
    const ADMIN_RETURN_GOODS_CODE = '管理员{admin_name},您有新的退货订单,单号{order_id}';
    //管理员支付成功提醒
    const ADMIN_PAY_SUCCESS_CODE = '管理员{admin_name},您有新的订单,已支付成功,单号{order_id}';
    //管理员确认收货
    const ADMIN_TAKE_DELIVERY_CODE = '管理员{admin_name},单号{order_id},用户确认收货';
    //推广人短信通知
    const PROMOTER_PAY_SUCCESS = '您的客户已支付成功,单号{order_id},前往小程序查看';

    public function __construct() {
        
    }

    /**
     * 
     * @author Tiger <1192851302@qq.com>
     * @param type $code
     * @return type
     */
    public static function getConstants($code = '') {
        $oClass = new \ReflectionClass(__CLASS__);
        $stants = $oClass->getConstants();
        if ($code) {
            return isset($stants[$code]) ? $stants[$code] : '';
        } else {
            return $stants;
        }
    }

    /**
     * @author Tiger <1192851302@qq.com>
     * @param type $phone
     * @param type $content
     * @return type
     */
    public static function qunaiSend($phone, $content) {
        $config = system_config(['system_smsbao_username', 'system_smsbao_token', 'system_smsbao_sign']);
        $param = [
            'username' => $config['system_smsbao_username'],
            'secret' => $config['system_smsbao_token'],
            'to' => $phone,
            'content' => '【' . $config['system_smsbao_sign'] . '】' . $content
        ];
        $return = HttpService::instance()->get(self::$SMSUrl . "/api/sms/send", $param);
        if ($return['code'] == '0') {
            return ['msg' => $return['desc'], 'status' => 200];
        }
        return ['msg' => $return['desc'], 'status' => 400];
    }

    /**
     * @author Tiger <1192851302@qq.com>
     * @param type $phone
     * @param type $template
     * @param array $param
     * @return type
     */
    public static function send($phone, $template, array $param) {
        foreach ($param as $key => $item) {
            $template = str_replace('{' . $key . '}', $item, $template);
        }
        $template = str_replace(['福利', '新茶', '直播', '进口', '老虎', '渝妹', '直销', '肾', '余额', '平安', '代购'
                ], '*', $template);
        return self::qunaiSend($phone, $template);
    }

}
